home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageioproc.lib / dev / examples / doimagenewbuffer / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-17  |  5.7 KB  |  236 lines

  1. /* This example use of imageprocess.library loads the image file specified
  2.         on the command line into a new buffer and views it halved in size in a
  3.         guigfx window.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19.  
  20. #include <imageio/imageio.h>
  21. #include <imageio/imageio_protos.h>
  22. #include <imageio/imageio_pragmas.h>
  23.  
  24. #include <imageprocess/imageprocess.h>
  25. #include <imageprocess/imageprocess_protos.h>
  26. #include <imageprocess/imageprocess_pragmas.h>
  27.  
  28. #include <guigfx/guigfx.h>
  29. #include <pragmas/guigfx_pragmas.h>
  30. #include <guigfx/guigfx_protos.h>
  31.  
  32. /* Function prototypes */
  33. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  34. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  35.  
  36. extern struct Library *DOSBase;
  37. struct Library *ImageIOBase, *IntuitionBase, *ImageProcessBase;
  38.  
  39. void main( int argc, char **argv )
  40. {
  41.     if ( argv[1] )
  42.     {
  43.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  44.         ImageProcessBase = OpenLibrary( "imageprocess.library", 1 );
  45.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  46.         if ( IntuitionBase && ImageIOBase && ImageProcessBase )
  47.         {
  48.             struct ImageHandle *ih;
  49.             ULONG err;
  50.  
  51.             err = AllocImage( &ih,
  52.                 IMG_SrcFilename, argv[1],
  53.                 TAG_DONE );
  54.             if ( !err )
  55.             {
  56.                 ULONG num = 1, denom = 2;
  57.                 ULONG x, y, bpp, buffersize;
  58.                 UBYTE *buffer, cs;
  59.                 int prevpercent;
  60.  
  61.                 err = GetImageAttrs( ih,
  62.                     IMG_Width, &x,
  63.                     IMG_Height,&y,
  64.                     IMG_BytesPerPixel, &bpp,
  65.                     IMG_ColourSpace, &cs,
  66.                     IMG_TestScaleNum, num,
  67.                     IMG_TestScaleDenom, denom,
  68.                     TAG_DONE );
  69.                 if ( !err )
  70.                 {
  71.                     printf( "width=%ld, height=%ld\n", x, y );
  72.                     printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  73.  
  74.                     prevpercent = 0;
  75.  
  76.                     err = ReadImage( ih,
  77.                         IMG_ScaleNum, num,
  78.                         IMG_ScaleDenom, denom,
  79.                         IMG_ImageBuffer, &buffer,
  80.                         IMG_ProgressHook, progressFunc,
  81.                         IMG_ProgressUserData, &prevpercent,
  82.                         TAG_DONE );
  83.                     if ( !err )
  84.                     {
  85.                         ULONG iperr;
  86.  
  87.                         printf("imageprocess.library DoImageProcess() new buffer\n");
  88.  
  89.                         iperr = DoImageProcess( IMP_Process_Scale,
  90.                             IMP_SourceBuffer ,buffer,
  91.                             IMP_SourceBytesPerPixel, bpp,
  92.                             IMP_SourceColourSpace, cs,
  93.                             IMP_SourceWidth, x,
  94.                             IMP_SourceHeight, y,
  95.                             IMP_ScaleDenom, 2,
  96.                             IMP_ScaleType, IPSCALE_FAST,
  97.                             IMP_ScaleMaintainAspect, FALSE,
  98.                             IMP_Width, &x,
  99.                             IMP_Height, &y,
  100.                             IMP_Buffer, &buffer,
  101.                             IMP_BufferSize, &buffersize,
  102.                             TAG_DONE );
  103.                         if ( !iperr )
  104.                         {
  105.                             printf("%ld x %ld\n",x,y);
  106.  
  107.                             DisplayGuiGfx( buffer, cs, x * bpp, x, y );
  108.  
  109.                             FreeMem( buffer, buffersize );
  110.                         }
  111.                         else printf( "doimageprocess error:%ld\n", iperr );
  112.                     }
  113.                     else printf( "read image error:%d\n", err );
  114.                 }
  115.                 else printf( "get image attrs error:%d\n", err );
  116.  
  117.                 FreeImage( ih );
  118.             }
  119.             else printf( "alloc image error:%d\n", err );
  120.         }
  121.  
  122.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  123.         if ( ImageProcessBase ) CloseLibrary( ImageProcessBase );
  124.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  125.     }
  126. }
  127.  
  128. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  129. {
  130.     int *prevpercent = (int *)userdata;
  131.  
  132.     int percent = ( curr * 100 ) / lines;
  133.  
  134.     if ( *prevpercent != percent )
  135.     {
  136.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  137.     }
  138.  
  139.     *prevpercent = percent;
  140.  
  141.     return NULL;
  142. }
  143.  
  144. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  145. {
  146.     struct Library *GuiGFXBase;
  147.  
  148.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  149.     if ( GuiGFXBase )
  150.     {
  151.         struct Window *win;
  152.         APTR dh, pi;
  153.         UBYTE *argb;
  154.  
  155.         win = OpenWindowTags( NULL,
  156.             WA_Title, "Proof",
  157.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  158.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  159.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  160.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  161.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  162.             WA_Left, 16,
  163.             WA_Top, 16,
  164.             WA_Width, x,
  165.             WA_Height, y,
  166.             TAG_DONE );
  167.  
  168.         if ( win != NULL )
  169.         {
  170.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  171.  
  172.             if ( dh )
  173.             {
  174.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  175.  
  176.                 if ( argb )
  177.                 {
  178.                     int i, count = 0;
  179.                     char **dest;
  180.  
  181.                     /* Convert a buffer to an ARGB buffer setting A to 0.*/
  182.                     dest = (char **)argb;
  183.  
  184.                     switch ( cs )
  185.                     {
  186.                         case IMCS_RGB:
  187.                             for ( i = 0; i < x * y * 3; i += 3 )
  188.                             {
  189.                                 dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  190.                             }
  191.                         break;
  192.  
  193.                         case IMCS_GREY:
  194.                             for ( i = 0; i < x * y; i++ )
  195.                             {
  196.                                 argb[count++] = 0;
  197.                                 argb[count++] = buffer[i];
  198.                                 argb[count++] = buffer[i];
  199.                                 argb[count++] = buffer[i];
  200.                             }
  201.                         break;
  202.                     }
  203.  
  204.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  205.  
  206.                     if ( pi )
  207.                     {
  208.                         struct Message *msg;
  209.  
  210.                         DrawPicture( dh, pi, 0, 0, NULL );
  211.  
  212.                         Wait( 1L << win->UserPort->mp_SigBit );
  213.  
  214.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  215.  
  216.                         DeletePicture( pi );
  217.                     }
  218.                     else printf( "failed to create picture\n" );
  219.  
  220.                     FreeVec( argb );
  221.                 }
  222.                 else printf( "failed to allocate argb buffer\n" );
  223.  
  224.                 ReleaseDrawHandle( dh );
  225.             }
  226.             else printf( "failed to get drawhandle\n" );
  227.  
  228.             CloseWindow( win );
  229.         }
  230.         else printf( "failed to open window\n" );
  231.     }
  232.     else printf( "failed to open guigfx.library\n" );
  233.  
  234.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  235. }
  236.